//----------------------------------
//------Soy thrower-----------------
//-----------Made by Luafox---------
//----------------------------------
//---Part of the SMOD Weapons Pack--
//----------------------------------
if ( SERVER ) then
	AddCSLuaFile( "cl_init.lua" )	
	AddCSLuaFile( "shared.lua" )
	SWEP.HoldType			= "grenade"
end

if ( CLIENT ) then
	--Settings
	SWEP.Author		= "Luafox"
	SWEP.PrintName 		= "Soybeans"
	SWEP.Slot			= 0
	SWEP.SlotPos		= 7
	SWEP.DrawAmmo 		= false
	SWEP.DrawCrosshair	= false
	SWEP.Category			= "SMOD"
	SWEP.WepSelectIcon = surface.GetTextureID("weapons/soyicon")


	-- Kill Icon
	if(file.Exists("../materials/weapons/soy_killicon.vmt")) then
		killicon.Add("smod_soybean","weapons/soy_killicon",Color(255,255,255))
	end 

end
SWEP.ViewModel		= "models/weapons/v_mamehands.mdl"
SWEP.WorldModel		= "models/weapons/w_mamehands.mdl"
SWEP.Weight		= 5
SWEP.AutoSwitchTo		= true
SWEP.AutoSwitchFrom		= false
SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true
--Info
SWEP.Contact		= "Maybe later"
SWEP.Purpose		= "Soybeans are good for you."
SWEP.Instructions	= "Throw soybeans"

--Variables--
SWEP.PrimeroDelay = CurTime()

function SWEP:Initialize()
end

function SWEP:Precache()
util.PrecacheSound("weapons/mamehand/soy.wav")
util.PrecacheSound("weapons/iceaxe/iceaxe_swing1.wav")

end

//idleanimation controlled by timers
function SWEP:DoIdle(seconds)
	timer.Destroy("SoyIdle")
	timer.Create("SoyIdle", seconds, 1, function() self:SendWeaponAnim(ACT_VM_IDLE); end)
end

function SWEP:PrimaryAttack()
	if self.PrimeroDelay < CurTime() then
		self.PrimeroDelay = CurTime()+ 1.25
		self:SendWeaponAnim(ACT_VM_PRIMARYATTACK);
		self:DoIdle(1); -- make the idle animation
		
		timer.Simple(0.604, self.Beans,self)
	end
end

//custom function for bean throwing
function SWEP:Beans()
		local Right = self.Owner:EyeAngles():Right();
		local Up = self.Owner:EyeAngles():Up();
		local Front = self.Owner:GetAimVector();
		
	if ( SERVER ) then -- only spawn things on server to prevent issues
		self.Weapon:EmitSound(Sound("weapons/iceaxe/iceaxe_swing1.wav"))
		for I = 1, 12 do -- looping

			local Bean = ents.Create("smod_soybean");

			if ValidEntity(Bean) then

				Bean:SetPos(self.Owner:GetShootPos() + Front * 12 + Right * 15 + Up * 12);
				Bean:SetAngles(Front:Angle());
				Bean:SetCollisionGroup(COLLISION_NONE)
				Bean:Spawn();
				Bean:Activate();
				Bean:SetOwner(self.Owner) -- now we are the owner to prevent worldspawn getting the frag
				timer.Simple(0.35,function() Bean:SetCollisionGroup(COLLISION_GROUP_PROJECTILE) end); -- prevent colliding until the beans have flewn a bit
				
				local Physics = Bean:GetPhysicsObject();
	
				if ValidEntity(Physics) then
		
				local Random = Front:Angle();
						Random:RotateAroundAxis(Random:Up(),math.Rand(-3.7,3.7));
						Random:RotateAroundAxis(Random:Right(),math.Rand(-3.7,3.7));
						
						Random = Random:Forward();

						Physics:ApplyForceCenter(Random * 1100);
				end
			end
		end
	end
end

function SWEP:SecondaryAttack()
end

function SWEP:Think()
end

function SWEP:Reload()
end

//always return true on holster and deploy
function SWEP:Deploy()
	self:SendWeaponAnim(ACT_VM_DRAW);
	self:DoIdle(1); -- make idle animation
	return true
end
function SWEP:Holster()
	timer.Destroy("SoyIdle"); -- destroy the timer so it's not there the next time we pull out the weapon
	return true
end

-- Primary things
SWEP.Primary.ClipSize	= -1
SWEP.Primary.DefaultClip	= -1 
SWEP.Primary.Automatic	= true
SWEP.Primary.Ammo	= "none"

-- Secondary things
SWEP.Secondary.ClipSize	= -1
SWEP.Secondary.DefaultClip	= -1 
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo	= "none"
